import BaseModel from './BaseModel'
import { get, post } from '../api/ajax'
import API from '../api/apis'
import WorkModel from './WorkModel'
import { ImageFill, PixelUnitType } from '../utils/filters'

// 我的模型
class MeModel extends BaseModel {

  // 主页头像 String
  public home_icon = ''
  // 主页标题 String
  public home_title = ''
  // 简历链接 String
  public home_jianli = ''
  // 关于我  String
  public abouts = ''
  public aboutArr = []
  // 专业技能 String
  public expertise = ''
  public expertiseArr = []
  // ios Number
  public iosNum = 0
  // node Number
  public nodeNum = 0
  // web Number
  public webNum = 0
  // 小程序 Number
  public xcxNum = 0
  // iOS技能说明 String
  public iosShow = ''
  // web技能说明 String
  public webShow = ''
  // Node技能说明 String
  public nodeShow = ''
  // 小程序技能说明 String
  public xcxShow = ''
  // 工作经验说明 String
  public experience_show = ''
  public experience_showArr = []
  // 工作经验
  public experiences = []
  // 公司
  public works: WorkModel[] = []
  // 工作经历说明
  public works_show = ''
  public works_showArr: string[] = []

  // 构造方法
  constructor() {
    super()
  }

  // 加载所有反馈数据
  public static getModel() {
    return new Promise((resolve, reject) => {
      get(API.me.get).then((data: any) => {
        const m = this.creatModelWithJson(data) as MeModel
        m.aboutArr = data.abouts.split('\n')
        m.expertiseArr = data.expertise.split('\n')
        m.works_showArr = data.works_show.split('\n')
        m.experience_showArr = data.experience_show.split('\n')
        m.works = WorkModel.getModelsWithArr(data.works) as WorkModel[]
        resolve(m)
      }).catch((err) => {
        reject(err)
      })
    })
  }


  /* ----- 获取指定图片大小 -------*/
  // 手机端首页图片
  get mMeHomeIconUrl() {
    return ImageFill(this.home_icon, 3, 3, PixelUnitType.rem)
  }

  get pcMeHomeIconUrl() {
    return ImageFill(this.home_icon, 150, 150)
  }

  // 修改数据
  public modifyData() {
    return new Promise((resolve, reject) => {
      post(API.me.update, this.getJson(['home_icon', 'home_title', 'home_jianli',
        'abouts', 'expertise', 'iosNum', 'nodeNum', 'webNum',
        'xcxNum', 'iosShow', 'webShow', 'nodeShow', 'xcxShow',
        'experience_show', 'works_show'])).then((data) => {
        resolve(data)
      }).catch((err) => {
        reject(err)
      })
    })
  }
}

export default MeModel
